package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyThemeQuery;
import cn.wolfcode.wolf2w.repository.StrategyThemeRepository;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {

    @Autowired
    private StrategyThemeRepository strategyThemeRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(StrategyTheme strategyTheme) {
        strategyTheme.setId(null);
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public void delete(String id) {
        AssertUtil.hasLength("请传入正确的id",id);
        strategyThemeRepository.deleteById(id);
    }

    @Override
    public void update(StrategyTheme strategyTheme) {
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public StrategyTheme findByOne(String id) {
        AssertUtil.hasLength("请传入正确的id");
        return strategyThemeRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyTheme> findAll() {
        return strategyThemeRepository.findAll();
    }

    /**
     * 高查
     * @param qo
     * @return
     */
    @Override
    public Object query(StrategyThemeQuery qo) {
        Query query = new Query();
        long count = mongoTemplate.count(query, StrategyTheme.class);
        if (count==0) {
            return Page.empty();
        }
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        count = mongoTemplate.count(query, StrategyTheme.class);
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageRequest);
        List<StrategyTheme> list = mongoTemplate.find(query, StrategyTheme.class);
        return new PageImpl<>(list,pageRequest,count);
    }

    @Override
    public void saveOrUpdate(StrategyTheme strategyTheme) {
        AssertUtil.hasNumber("请输入正确的状态值",strategyTheme.getState(),strategyTheme.STATE_NORMAL,strategyTheme.STATE_DISABLE);
        AssertUtil.hasLength("请输入主体",strategyTheme.getName());
        AssertUtil.hasNumber("请输入正确的序号",strategyTheme.getSequence(),0,1000);
        if (StringUtils.hasLength(strategyTheme.getId())) {
            this.update(strategyTheme);
        } else {
            this.save(strategyTheme);
        }
    }

    @Override
    public JsonResult changeHotValue(String id, String hot) {
        return null;
    }
}
